package MyPokerDemo;

import java.util.*;

/**
 * 三人斗地主
 * 1\初始化玩家名字（后找方法或者getter、setter方法）
 * 2、初始化扑克牌（run方法）
 * 3、洗牌发牌   （dealCards方法）
 * 4、打印输出玩家的牌（showPokers和printPokers方法）
 */
public abstract class PokerImpl implements Poker {
    //花色
    String[] colors = {"♥", "♣", "♠", "♦"};
    //数字
    String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
    //存放扑克牌的map
    Map<Integer, String> pokers = new HashMap<>();
    //记录牌的编号
    List list = new ArrayList<>();

    //底牌和参与斗地主的三人
    List liuyifei = new ArrayList<>();
    List zhaoliying = new ArrayList<>();
    List xiaohei = new ArrayList<>();
    List dipai = new ArrayList<>();

    private String Aperson = "liuyifei";
    private String Bperson = "zhaoliying";
    private String Cperson = "xiaohei";
    private String bottom = "dipai";

    public PokerImpl() {
    }

    public PokerImpl(String Aperson, String Bperson, String Cperson) {
        this.Aperson = Aperson;
        this.Bperson = Bperson;
        this.Cperson = Cperson;
    }

    public void setAperson(String aperson) {
        Aperson = aperson;
    }

    public void setBperson(String bperson) {
        Bperson = bperson;
    }

    public void setCperson(String cperson) {
        Cperson = cperson;
    }

    public void setBottom(String bottom) {
        this.bottom = bottom;
    }

    public String getAperson() {
        return Aperson;
    }

    public String getBperson() {
        return Bperson;
    }

    public String getCperson() {
        return Cperson;
    }

    public String getBottom() {
        return bottom;
    }

    //初始化扑克牌
    @Override
    public void initial() {
        int num = 0;
        for (String number : numbers) {
            for (String color : colors) {       //每一张牌都有四种花色
                String poker = color + number;
                pokers.put(num, poker);

                list.add(num);
                num++;
            }
        }

        pokers.put(num, "小王");
        list.add(num++);
        pokers.put(num, "大王");
        list.add(num);

        System.out.println("所有的牌：" + pokers);
        System.out.println("牌的编号:" + list);
    }

    @Override
    public void dealCards() {
        //洗牌
        Collections.shuffle(list);
        System.out.println("洗好牌后，编号为" + list);
        //开始发牌
        for (int i = 0; i < list.size(); i++) {
            Integer pokerNum = (Integer) list.get(i);
            if (i >= list.size() - 3) {
                dipai.add(pokerNum);
            } else if (i % 3 == 0) {
                liuyifei.add(pokerNum);
            } else if (i % 3 == 1) {
                zhaoliying.add(pokerNum);
            } else if (i % 3 == 2) {
                xiaohei.add(pokerNum);
            }
        }
        System.out.println();
    }

    @Override
    public void showPokers() {
        System.out.println("-------------------------------");
        System.out.println(Aperson + ":" + printPoker(liuyifei));
        System.out.println(Bperson + ":" + printPoker(zhaoliying));
        System.out.println(Cperson + ":" + printPoker(xiaohei));
        System.out.println(bottom + ":" + printPoker(dipai));
    }

    //对发到的牌排序后打印输出
    @Override
    public String printPoker(List nums) {
        Collections.sort(nums);
        StringBuilder sb = new StringBuilder();
        for (Object num : nums) {
            String poker = pokers.get(num);
            sb.append(poker).append(" ");
        }
        String str = sb.toString();
        return str.trim();
    }
}
